<?php
# file that will process and save to database new user

# include of site functions
include_once $_SERVER["DOCUMENT_ROOT"].'Meet-n-go/php/includes.php'; 	// contains includes of all files with functions
session_start();					// starts session for current user
c_db_connect(); 					// connect to database

# make sure all data was entered
if(!isset($_GET['group_id']) || empty($_GET['group_id'])) i_set_msg_err('Error in group id');
else{
	$group_id = c_escape($_GET['group_id']); // escape before doing something with it
}

if( !has_errors() ){
	#get group type
	$query = "SELECT TYPE FROM groups WHERE ID = $group_id";
	$result = c_db_query($query);
	
	$nr_rows = mysql_num_rows($result);
	if($nr_rows > 0){
		$row = mysql_fetch_array($result);
		
		$type = $row[0];
	}
	else i_set_msg_err('Incorrect request');
}

if( !has_errors() ){
	$user_id = $_SESSION['user_id'];
	
	if($type == 0) $status = 1; // for public group status automately are acceped
	elseif($type == 1) $status = 0; // for private group status at start are pending
	
	#add new participant to group
	$query = "INSERT INTO participants (USER_ID, GROUP_ID, STATUS) VALUES ('$user_id', '$group_id', '$status')";
	if(!c_db_query($query)) i_set_msg_err('Error in database'); // problem with database
	else{
		if($status == 1) i_set_msg_inf('Successfully joined to group');
		elseif($status == 0) i_set_msg_inf('Request sent to group master');
	}
}

c_db_disconnect(); 					// disconnect from database

# Where to go after processing
$page_id = I_SEARCH_GROUP; // go back to search group page


# code that makes sure, this page is being redirected from afterwards
echo '<head><meta http-equiv="refresh" content="0; url=../../../index.php?id=' . $page_id . '" /></head>';


?>